package be.kuleuven.peno3.mobiletoledo.view.campusnetlogin;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import be.kuleuven.peno3.mobiletoledo.R;
import be.kuleuven.peno3.mobiletoledo.Data.UserDAO;
import be.kuleuven.peno3.mobiletoledo.model.User;

public class EditData extends Activity {
	private EditText usernameET, passwordET;

	/**
	 * Initialize the Activity.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.campusnetlogineditlayout);
		
		TextView bar = (TextView) findViewById(R.id.edit_login_bar);
		bar.setText("Login bewerken");
		usernameET = (EditText) findViewById(R.string.UsernameField);
		passwordET = (EditText) findViewById(R.string.PasswordField);
		Button b = (Button) findViewById(R.string.SaveButton);
		UserDAO userManager = new UserDAO(EditData.this);
		userManager.open();
		usernameET.setText(userManager.fetchProperty("userid"));
		passwordET.setText(userManager.fetchProperty("password"));
		userManager.close();
		
		b.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				UserDAO userManager = new UserDAO(EditData.this);
				userManager.open();
				User user=User.getInstance();
				
				String userid = usernameET.getText()+"";
				String password = passwordET.getText()+"" ;
				
				if(user.getIdNumber()!=null) {
					userManager.updateEntry("userid", userid);
					userManager.updateEntry("password", password);
				} 
				else {
					userManager.createEntry("userid", userid);
					userManager.createEntry("password", password);
				}
				
				user.setIdNumber(userid);
				user.setPassword(password);
				
				userManager.close();
				finish();
			}
		});
	}

}
